package com.igeekhome.ccs.biz.impl;

import com.igeekhome.ccs.biz.ICustomerServiceGroupBiz;
import com.igeekhome.ccs.entity.CustomerService;
import com.igeekhome.ccs.entity.CustomerServiceGroup;
import com.igeekhome.ccs.mapper.CustomerServiceGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
//告诉springboot,这是一个业务对象
public class CustomerServiceGroupBizImpl implements ICustomerServiceGroupBiz {

    @Autowired
    private CustomerServiceGroupMapper mapper;
//👇zyh-----------------------------------------------
    //查询所有客服组
    @Override
    public List<CustomerServiceGroup> query(){
        return mapper.query();
    }

    //通过客服组id得到客服对象
    @Override
    public List<CustomerService> queryCsInfoByGroupId(int groupId) {
        return mapper.queryCsInfoByGroupId(groupId);
    }

    //模糊匹配
    @Override
    public List<CustomerServiceGroup> matchCsgwithString(String str) {
        List<CustomerServiceGroup> totalList = mapper.query();
        List<CustomerServiceGroup> resultList = new ArrayList<CustomerServiceGroup>();
        Iterator<CustomerServiceGroup> iter = totalList.iterator();
        str=str.trim();
        for(int i = 0 ; i<totalList.size(); i++) {
                      //模糊匹配、大小写均可
            Pattern pattern = Pattern.compile(str, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(totalList.get(i).getGroupName());
            if (matcher.find() ) {
                resultList.add(totalList.get(i));
            }
        }
        return resultList;
    }

    //删除单个客服组
    @Override
    public void deleteCsgById(int groupId) {
        mapper.deleteCsgById(groupId);
    }

    //批量删除客服组
    @Override
    public void deleteCsgByList(List<Integer> list) {
        for(int i = 0;i<list.size();i++){
            mapper.deleteCsgById(list.get(i));
        }
    }

    //新增客服组
    @Override
    public int createNewCsg(String str) throws ParseException {
        CustomerServiceGroup customerServiceGroup = new CustomerServiceGroup();
        //id自增

        //名字去空格
        str=str.trim();
        customerServiceGroup.setGroupName(str);
        //初始化人数
        customerServiceGroup.setGroupNumber(0);

        //创建时间
        Date now=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strNow = df.format(now);
        Date dateNow = df.parse(strNow);
        customerServiceGroup.setCreateTime((java.sql.Date) dateNow);



        mapper.createNewCsg(customerServiceGroup);

        return 0;
    }

    //编辑客服组
    @Override
    public int updateCsg(String str, CustomerServiceGroup customerServiceGroup) throws ParseException {
        //更新时间
        Date now=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strUpdate = df.format(now);
        Date dateUpdate = df.parse(strUpdate);

        str=str.trim();
        mapper.updateCsg(str,dateUpdate,customerServiceGroup.getGroupId());
        return 0;
    }
//👆zyh-------------------------------------------------
}
